# -*- coding: utf-8 -*-

"""
 (c) 2023 - Copyright CTyunOS Inc

 Authors:
   youyifeng <youyf2@chinatelecom.cn>

"""

from optparse import OptionParser
from cve_ease import activate_session
import logging

logger = logging.getLogger('cve-ease')


def get_usage_str(usage):
    return usage + "\n(Specify the --help global option for a list of other help options)"


def check_enabled(gconfig):
    if 'autoupdate' in gconfig.config and \
            'enabled' in gconfig.config['autoupdate'] and \
            '1' == gconfig.config['autoupdate']['enabled']:
        return True
    return False


def handle_autoupdate(gconfig, db_session, args):
    """[basic] Auto update rpm package"""

    usage = "usage: %prog autoupdate <options>"
    parser = OptionParser(usage=get_usage_str(usage))

    parser.add_option('-v', '--verbose', dest='verbose', action='store_true', default=False,
                      help='show verbose output')

    (options, args) = parser.parse_args(args)

    session = activate_session(db_session, gconfig)
    if gconfig.debug:
        print(" * active sql session")

    if not check_enabled(gconfig):
        print("autoupdate is not enabled! you can modify %s to enable it." % gconfig.configFile)
        exit(0)

    print('autoupdate is enabled')

    level = ['low', 'moderate', 'important', 'critical']
    if 'level' in gconfig.config['autoupdate'] and gconfig.config['autoupdate']['level'] in level:
        print('autoupdate level is %s' % gconfig.config['autoupdate']['level'])
    else:
        print('autoupdate in default level critical')


